#include "armorPiece.hpp"
#include "publicUtil.hpp"

// armorPiece::armorPiece(/* args */)
// {
// }

// armorPiece::~armorPiece()
// {
// }

void armorPiece::initial(RotatedRect rotate_Rect1,RotatedRect rotate_Rect2)
{
    Point center1 = getRotateRectCenter(rotate_Rect1);
    Point center2 = getRotateRectCenter(rotate_Rect2);
    if(center1.x < center2.x)
    {
        this->left  = rotate_Rect1;
        this->right = rotate_Rect2;
    }
    else
    {
        this->right = rotate_Rect1;
        this->left  = rotate_Rect2;
    }
    this->center = Point((center1.x+center2.x)/2,(center1.y+center2.y)/2);
}

void armorPiece::initial(Rect rect1,Rect rect2)
{
    Rect Left,Right;
    if(rect1.x < rect2.x)
    {
        Left  = rect1;
        Right = rect2;
    }
    else
    {
        Left  = rect2;
        Right = rect1;
    }
    // 默认rect1已经是左矩阵了，rect2默认为右矩阵
    this->leftUp     = Left.tl();
    this->rightUp    = Right.tl();
    this->leftDown   = Left.br();
    this->rightDown  = Right.br();

    this->rightUp.x  = this->rightUp.x + (rect2.width<rect2.height?rect2.width:rect2.height);
    this->leftDown.x = this->leftDown.x - (rect1.width<rect1.height?rect1.width:rect1.height);
    
    this->width      = getDistance(this->leftUp,this->rightUp);
    this->height     = getDistance(this->leftUp,this->leftDown);

    this->area       = this->width*this->height;
    
    // this->score      = this->area + (this->width/this->height)*20;
}

void armorPiece::drawArmor(cv::OutputArray Picture_raw)
{
    line(Picture_raw.getMat(), this->leftUp,    this->leftDown,     Scalar(255,255,255), 1);
    line(Picture_raw.getMat(), this->rightUp,   this->rightDown,    Scalar(255,255,255), 1);
    line(Picture_raw.getMat(), this->leftUp,    this->rightUp,      Scalar(255,255,255), 1);
    line(Picture_raw.getMat(), this->leftDown,  this->rightDown,    Scalar(255,255,255), 1);
}